import { defineStore } from "pinia";
import request from "@/utils/request";
import { ref } from "vue";
import type { ApiRes, Profile } from "@/types/data";

const useUserStore = defineStore(
  "user",
  () => {
    const userInfo = ref({} as Profile);

    // 账号登录
    const login = async (data: any) => {
      const res = await request.post<ApiRes<Profile>>("/login", data);
      userInfo.value = res.data.result;
    };

    // 获取验证码
    const sendMobileMsg = async (mobile: string) => {
      await request.get("/login/code", {
        params: { mobile },
      });
    };

    // 验证码登录
    const mobileLogin = async (data: any) => {
      const res = await request.post<ApiRes<Profile>>("/login/code", data);
      userInfo.value = res.data.result;
    };

    // 退出
    const logout = () => {
      userInfo.value = {} as Profile;
    };

    return {
      userInfo,
      login,
      sendMobileMsg,
      mobileLogin,
      logout,
    };
  },
  {
    persist: true,
  }
);

export default useUserStore;
